<?php
/**
* Converts a path to a directory name
*/
function directory_name($directory='/tmp')
{
	$directory = str_replace("\\", '/', realpath($directory));
	return $directory;
}

/**
* Returns encrypted name of a directory
*/
function directory_name_encrypted($directory='/')
{
	//die($directory);
	return rawurlencode(base64_encode(str_replace("\\", '/', realpath($directory))));
}

/**
* Returns a readable name of a directory
*/
function directory_name_decrypted($directory_encrypted='')
{
	return base64_decode(rawurldecode($directory_encrypted));
}

/**
* Last modified time/date
*/
function last_modification($file='')
{
	$modified_on = '';
	if(is_file($file))
	{
		$modified_on = '- Last modified on: <strong>'.date('Y-m-d H:i:s', filemtime($file)).'</strong>';
	}
	
	return $modified_on;
}

/**
* Applies the breadcrumb
*/
function breadcrumb($directory='/tmp')
{
	$locations = explode('/', $directory);

	$href = '';
	foreach($locations as $l => $location)
	{
		$locations[$l] = $locations[$l];
		if($l!=0)
		{
			$href = $href.'/'.$locations[$l];
		}
		else
		{
			# On Windows styled file path, it includes the drive letter
			$href = $locations[$l];
		}
		$href_encrypted = directory_name_encrypted($href);
		$locations[$l] = "<a href='change-directory.php?directory={$href_encrypted}'>{$locations[$l]}</a>";
	}
	$breadcrumb = implode(__BREADCRUMB_SEPARATOR__, $locations);
	return $breadcrumb;
}

/**
* Reads out a particular directory
*/
function list_files($dir='/tmp')
{
	/**
	* Whatever location we enter, it should be a directory!
	*/
	if(!is_dir($dir)) return array("No directory: <strong class='error'>{$dir}</strong>");
	
	$html = array(
		'dir' => array(),
		'files' => array(),
	);
	
	$skip_by_name = array('.', '..', '.svn');
	$allow_by_extension = array('js', 'php', 'txt', 'css', 'html', 'htm', 'bat', 'sh', 'lrc');
	
	$files = scandir($dir);
	$files = array_diff($files, $skip_by_name);
	
	foreach($files as $f => $file)
	{
		# Linux compatible names
		$fullfile = directory_name($dir).'/'.$file;
		if(is_dir($fullfile))
		{
			/**
			* Handle the directory
			*/
			$recurse=false;
			if($recurse==true)
			{
				$children = list_files($fullfile);
				if(count($children))
				{
					$html[] = "<a href='?directory={$file}' class='directory'>{$file}</a>";
					$html = array_merge($html, $children);
				}
			}
			else
			{
				$fullfile_encrypted = directory_name_encrypted($fullfile);
				$html['dir'][] = "<a href='change-directory.php?directory={$fullfile_encrypted}' class='directory'>{$file}</a>";
				# We do not recurse
			}
		}
		else
		{
			/**
			* Handle the files
			*/
			#$html[] = $fullfile;
			$pathinfo = pathinfo($fullfile);

			# Choose some valid extensions to allow edit
			if(!empty($pathinfo['extension']) && in_array($pathinfo['extension'], $allow_by_extension))
			{
				$html['files'][] = "<a href='?edit=true&amp;file={$fullfile}' class='file'>{$file}</a>";
			}
		}
	}
	
	#print_r($html); die();
	return array_merge($html['files'], $html['dir']);
}
?>